package com.neusoft.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.neusoft.po.WalletTrans;
import com.neusoft.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.mapper.WalletMapper;
import com.neusoft.po.Wallet;
import com.neusoft.service.WalletService;

import static com.neusoft.util.CommonUtil.getCurrentTime;

@Service
public class WalletServiceImpl implements WalletService {


    @Autowired
    private WalletMapper walletMapper;

    @Override
    public int saveWallet(String userId) {
        return walletMapper.saveWallet(userId);
    }

    @Override
    public Wallet getWalletByUserid(String userId) {
        return walletMapper.getWalletByUserid(userId);
    }

    @Override
    public int updateMoney(String userId, BigDecimal account) {
        return walletMapper.updateMoney(userId, account);
    }

    @Override
    public int operate(String userId,BigDecimal amount,Integer type){
        BigDecimal money = getWalletByUserid(userId).getMoney();
        if(type==0){
            walletMapper.saveTransaction(userId,getCurrentTime(),amount,type,userId,"user");
            return walletMapper.updateMoney(userId,money.add(amount));
        } else if(type==1){
            walletMapper.saveTransaction(userId,getCurrentTime(),amount,type,"user",userId);
            return walletMapper.updateMoney(userId,money.subtract(amount));
        } else{
            walletMapper.saveTransaction(userId,getCurrentTime(),amount,type,"business",userId);
            return walletMapper.updateMoney(userId,money.subtract(amount));
        }
    }

    @Override
    public List<WalletTrans> listWalletTransByUserId(String userId) {
        return walletMapper.listWalletTransByUserId(userId);
    }

}
